//
//  ViewController.swift
//  FoundationDemo
//
//  Created by ZhangBo on 2017/5/8.
//  Copyright © 2017年 ZhangBo. All rights reserved.
//

import UIKit

import Foundation

//测试结构体
func testStruct()
{
    let range = NSRange.init(location: 3, length: 5)//NSRange表示一个范围
    print(range)//输出(3,5)
    print(NSStringFromRange(range))//按格式是输出(输出{3，5})
    
    let point = CGPoint.init(x: 4, y: 6)//NSPoint表示一个位置
    print(point)//输出(4.0,6.0)
    
    let size = CGSize.init(width: 100, height: 200)//NSSize表示大小(宽、高)
    print(size)
    
    let rect = CGRect.init(x: 4, y: 6, width: 100, height: 200)//NSRect表示一个区域(x起点，y起点，宽，高)
    print(rect)
}

//测试日期
func testDate()
{
    let currentDate = Date()//获取当前日期
    print(currentDate)//打印utc时间(2016-01-17 15:09:04 +0000)
    
    let dateFormatter = DateFormatter() //日期格式化
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    print(dateFormatter.string(from: currentDate))//打印本地化的时间（2016-01-17 23:09:04）
    
    var newDate = Date()
    newDate = newDate.addingTimeInterval(100)//在当前日期的基础上加上100秒
    print(newDate)
    
    let futureDate = Date.distantFuture //获取随机的一个将来的日期
    print(futureDate)
    
    let timeInterval = newDate.timeIntervalSince(currentDate)//获取时间差
    print(timeInterval)
    
    let earlierDate = (newDate as NSDate).earlierDate(currentDate)//获取两个日期中比较早的日期
    print(earlierDate)
    
    let myDate = dateFormatter.date(from: "2016-01-22 12:00:00")//从字符串获取日期
    print(myDate!)
}

//测试NSString
func testString()
{
    let nsString = NSString(format: "%@", "test NSString") //NString表示一个字符串
    print(nsString)
    print(nsString.hasPrefix("test"))//是否有某个前缀
    print(nsString.range(of: "NSS"))//获取子字符串的位置
    print(nsString.substring(from: 3))//获取从第3个位置开始到末尾的子字符串,编译出错，参数传Index类型，但是文档是Int
    print(nsString.length) //字符串的长度(是字符数，不是字节数)
    
    let swiftString:String = "test Swift String"//使用Swfit内置的String
    print(swiftString)
    
    let swiftStringTest = "test Swift StringTest"//自动推断类型为String
    print(swiftStringTest)
}

//测试文件读写
func testFile()
{
    let path = "/Users/lingyun/Desktop/test.txt"//文件读取，将文件放在桌面
    do{
        let strContent = try NSString(contentsOfFile: path,encoding:String.Encoding.utf8.rawValue)
        print(strContent)
    }
    catch {
        print("文件读取错误")
    }
    
    let writePath = "/Users/lingyun/Desktop/testWrite.txt"//文件写入，将文件放在桌面
    let testString = NSString(format: "test Swfit file write.")
    do{
        try testString.write(toFile: writePath, atomically: true, encoding:String.Encoding.utf8.rawValue)
    }
    catch{
        print("写入错误")
    }
}

//测试字典，使用Swift字典，而非NSDictionary
func testDictionary()
{
    var dic:Dictionary<String,String> = ["01":"小张","02":"小李"]
    print(dic)
    print(dic["01"]!)
    
    dic["03"] = "晓明"
    dic.updateValue("小明", forKey: "03")
    dic.removeValue(forKey: "01")
    
    for(key,value) in dic
    {
        print("键:\(key) 值:\(value)")
    }
}


class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        testStruct()
        testDate()
        testString()
        testFile()
        testDictionary()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

